package com.example.demo.service.impl;

import com.example.demo.entity.Coalyieldmonitoring;
import com.example.demo.handle.YieldMonitoringListParseHandler;
import com.example.demo.mapper.CoalyieldmonitoringMapper;
import com.example.demo.service.CoalyieldmonitoringService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-26
 */
@Service
public class CoalyieldmonitoringServiceImpl extends ServiceImpl<CoalyieldmonitoringMapper, Coalyieldmonitoring> implements CoalyieldmonitoringService {

    @Autowired
    private CoalyieldmonitoringMapper coalyieldmonitoringMapper;

    @Override
    public Integer insertYieldData(String yieldXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            YieldMonitoringListParseHandler yieldMonitoringListParseHandler = new YieldMonitoringListParseHandler();
            try {
                // 读取xml文件
                parser.parse(yieldXml, yieldMonitoringListParseHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + yieldMonitoringListParseHandler.getCoalyieldmonitoringArrayList().size() + "个节点被存储");
            for (Coalyieldmonitoring coalyieldmonitoring : yieldMonitoringListParseHandler.getCoalyieldmonitoringArrayList()) {
                System.out.println(coalyieldmonitoring.getCsMineCode());
                System.out.println(coalyieldmonitoring.getCsDataTime());
                System.out.println(coalyieldmonitoring.getOmSystemModel());
                System.out.println(coalyieldmonitoring.getOmSystemName());
                System.out.println(coalyieldmonitoring.getOmSystemMaker());
                System.out.println(coalyieldmonitoring.getOmSystemProdate());
                System.out.println(coalyieldmonitoring.getOmSystemManum());
                System.out.println(coalyieldmonitoring.getOmSystemMaeffec());
                System.out.println(coalyieldmonitoring.getOmSystemInstime());
                System.out.println(coalyieldmonitoring.getOmUpdateDate());
                System.out.println(coalyieldmonitoring.getCsCapacityAnnual());
                System.out.println(coalyieldmonitoring.getOmCapacityMonth());
                System.out.println(coalyieldmonitoring.getOmCapacityDay());
                System.out.println(coalyieldmonitoring.getOmCapacityHour());
                System.out.println(coalyieldmonitoring.getOmConversionCft());
                System.out.println("----finish----");
                coalyieldmonitoringMapper.insertYieldData(coalyieldmonitoring.getCsMineCode(),coalyieldmonitoring.getCsDataTime(),coalyieldmonitoring.getOmSystemModel(),coalyieldmonitoring.getOmSystemName(),coalyieldmonitoring.getOmSystemMaker(),coalyieldmonitoring.getOmSystemProdate(),coalyieldmonitoring.getOmSystemManum(),coalyieldmonitoring.getOmSystemMaeffec(),coalyieldmonitoring.getOmSystemInstime(),coalyieldmonitoring.getOmUpdateDate(),coalyieldmonitoring.getCsCapacityAnnual(),coalyieldmonitoring.getOmCapacityMonth(),coalyieldmonitoring.getOmCapacityDay(),coalyieldmonitoring.getOmCapacityHour(),coalyieldmonitoring.getOmConversionCft());
            }
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
